package com.qst.yunpan.service.impl;

import com.qst.yunpan.dao.ShareMapper;
import com.qst.yunpan.entity.Share;
import com.qst.yunpan.entity.ShareExample;
import com.qst.yunpan.entity.ShareFile;
import com.qst.yunpan.entity.User;
import com.qst.yunpan.service.FileService;
import com.qst.yunpan.service.ShareService;
import com.qst.yunpan.utils.FileUtils;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Service
public class ShareServiceImpl implements ShareService {
    @Autowired
    private ShareMapper shareMapperDao;


    @Override
    public String shareFile(HttpServletRequest request, String currentPath, String[] shareFile) throws Exception {
        User user = new User();
        user = (User) request.getSession().getAttribute("user");

        String shareUrl = FileUtils.getUrl8();
        for (String file : shareFile) {
            Share share = new Share();
            share.setPath(currentPath + File.separator + file);
            share.setShareuser(user.getUsername());
            share.setShareurl(shareUrl);
            share.setStatus((byte) Share.PUBLIC);
            shareMapperDao.insert(share);
        }
        return shareUrl;
    }

    @Override
    public List<ShareFile> findShare(HttpServletRequest request, String shareUrl) throws Exception {
        ShareExample example = new ShareExample();
        example.createCriteria().andShareurlEqualTo(shareUrl).andStatusEqualTo((byte) Share.PUBLIC);
        List<Share> shares = shareMapperDao.selectByExample(example);

        for(Share share : shares) {
            System.out.println("*****************"+share.toString());
        }

        return getShareFile(request,shares);
    }

    @Override
    public List<ShareFile> getShareFile(HttpServletRequest request, List<Share> shares) {
        List<ShareFile> files = null;
        if(shares != null){
            files = new ArrayList<ShareFile>();
            String rootPath = request.getSession().getServletContext().getRealPath("/") + FileServiceImpl.PREFIX;
            for(Share share : shares){
                File file = new File(rootPath + share.getShareuser(),share.getPath());
                ShareFile shareFile = new ShareFile();
                shareFile.setFileType(FileUtils.getFileType(file));
                shareFile.setFileName(file.getName());
                shareFile.setFileSize(file.isFile() ? FileUtils.getDataSize(file.length()) :"-");
                shareFile.setLastTime(FileUtils.formatTime(file.lastModified()));
                shareFile.setShareUser(share.getShareuser());
                shareFile.setUrl(share.getShareurl());
                shareFile.setFilePath(share.getPath());
                files.add(shareFile);
            }
        }
        return files;
    }

    @Override
    public List<ShareFile> findShareByName(HttpServletRequest request, byte status) throws Exception {
        ShareExample example = new ShareExample();
        User user = (User) request.getSession().getAttribute("user");
        example.createCriteria().andShareuserEqualTo(user.getUsername()).andStatusEqualTo(status);
        List<Share> shares = shareMapperDao.selectByExample(example);
        return getShareFile(request,shares);
    }

    @Override
    public String cancelShare(String url, String filePath, byte status) throws Exception {
        if(Share.CANCEL == status){
            shareMapperDao.updateCancelShare(url,filePath, (byte) Share.DELETE);
            return "删除成功";
        }else{
            shareMapperDao.updateCancelShare(url,filePath, (byte) Share.CANCEL);
            return "链接已失效";
        }
    }
}
